package demo2;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyThread implements Runnable{
    static int piao = 100;
    static int num = 0;

    static Lock l = new ReentrantLock();
    @Override
    public void run() {
        while (true){
            l.lock();
            if (piao > 0) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() + "卖了" + piao + "号票");
                num++;
                piao--;
            } else {
                break;
            }
            l.unlock();
        }
    }

}
